var App = {
    SUCCESS_CODE: 200,
    FAIL_CODE: 400,
    ERROR_CODE: 500,
    basePath: "",//项目根目录
    currentUser:{},//当前在线用户
    get: function(url, param, success, fail, error){
        //ajax的get方法
        App.doAjax(url, param, 'get', true, '', success, fail, error);
    },
    post: function (url, paramObj, success, fail, error) {
        //ajax的post方法
        App.doAjax(url, paramObj, 'post', true, '', success, fail, error);
    },
    postJson: function (url, paramJson, success, fail, error) {
        // ajax的post方法发送json格式数据的方法
        App.doAjax(url, paramJson, 'post', true, 'application/json', success, fail, error);
    },
    getSync: function (url, param, success, fail, error) {
        //ajax的get方法
        App.doAjax(url, param, 'get', false, '', success, fail, error);
    },
    postSync: function (url, paramObj, success, fail, error) {
        //ajax的post方法
        App.doAjax(url, paramObj, 'post', false, '', success, fail, error);
    },
    postJsonSync: function (url, paramJson, success, fail, error) {
        // ajax的post方法发送json格式数据的方法
        App.doAjax(url, paramJson, 'post', false, 'application/json', success, fail, error);
    },
    doAjax: function (url, param, type, isAsync, contentType, handleSuccess, handleFail, handleError) {
        var result = '';
        $.ajax({
            url: url,
            data: param,
            async: App.isEmpty(isAsync) ? true : isAsync,//是否为异步
            type: App.isEmpty(type) ? "get" : type,
            contentType: App.isEmpty(contentType) ? "application/x-www-form-urlencoded" : contentType,
            success: function (res) {
                //请求成功时处理
                if (res.code === App.SUCCESS_CODE) {
                    if (handleSuccess) {
                        handleSuccess(res);
                    } else {
                        //统一处理成功
                        alert(res.msg)
                    }
                } else if (res.code === App.FAIL_CODE) {
                    if (handleFail) {
                        handleFail(res);
                    } else {
                        //统一处理失败
                        alert(res.msg)
                    }
                } else {
                    //内部异常，统一处理
                    alert(res.msg);
                }
                //拿取结果值
                result = res.data;
            },
            complete: function () {
                //请求完成的处理
                console.log("请求完成");
            },
            error: function () {
                console.log("error")
                if (handleError) {
                    handleError();
                } else {
                    //请求出错处理
                    console.log("请求出错", "请求出错");
                    alert("请求出错");
                }
            }
        })
        return result;
    },
    //判断是否为空
    isEmpty: function (val) {
        if (val != null && val != undefined) {
            val = val.toString();
        }
        if (val == null || val == undefined || val == '' || val.trim() == '') {
            return true;
        } else {
            return false;
        }
    },

    // 格式化时间戳 传入时间戳
    fomatTime: function (timestamp) {
        var date = new Date(timestamp);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    },

    setIframeHeight: function (iframe) {
        var ifm = document.getElementById(iframe);
        var subWeb = document.frames ? document.frames[iframe].document : ifm.contentDocument;
        if (ifm != null && subWeb != null) {
            ifm.height = subWeb.body.scrollHeight;
        }
    },

    getQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return (r[2]);
        return null;
    },


    require: function (selector, attr) {
        var msg = attr ? attr + "不能为空" : "请填完必填字段";
        var flag = true;
        $($(selector)).each(function () {
            if (App.isEmpty($(this).val())) {
                flag = false;
            }
        })
        if (!flag) {
            alert(msg);
        }
        return flag;
    }

}


